// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.view;

import org.dengues.core.CorePlugin;
import org.dengues.core.codegen.ICodeGenerator;
import org.dengues.core.components.EJETCodePart;
import org.dengues.designer.ui.editors.ProcessMultiEditor;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.designer.ui.process.models.CompNode;
import org.dengues.designer.ui.process.parts.CompNodePart;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
import org.eclipse.jdt.internal.ui.text.SimpleJavaSourceViewerConfiguration;
import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jdt.ui.text.IJavaPartitions;
import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
import org.eclipse.jdt.ui.text.JavaTextTools;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;

public class ProcessCodeView extends ViewPart implements ISelectionListener {

    private IDocument document;

    private final IAction viewStartAction = new Action() {

        @Override
        public String getText() {
            return Messages.getString("ProcessCodeView.Begin"); //$NON-NLS-1$
        }

        @Override
        public void run() {
            selectedCode = CODE_START;
            refresh();
        }
    };

    private final IAction viewMainAction = new Action() {

        @Override
        public String getText() {
            return Messages.getString("ProcessCodeView.Main"); //$NON-NLS-1$
        }

        @Override
        public void run() {
            selectedCode = CODE_MAIN;
            refresh();
        }
    };

    private final IAction viewEndAction = new Action() {

        @Override
        public String getText() {
            return Messages.getString("ProcessCodeView.End"); //$NON-NLS-1$
        }

        @Override
        public void run() {
            selectedCode = CODE_END;
            refresh();
        }
    };

    private final IAction viewAllAction = new Action() {

        @Override
        public String getText() {
            return Messages.getString("ProcessCodeView.All"); //$NON-NLS-1$
        }

        @Override
        public void run() {
            selectedCode = CODE_ALL;
            refresh();
        }
    };

    private static final int CODE_START = 0;

    private static final int CODE_MAIN = 1;

    private static final int CODE_END = 2;

    private static final int CODE_ALL = 3;

    private int selectedCode = CODE_ALL;

    private CompNode selectedNode;

    public ProcessCodeView() {
    }

    @Override
    public void createPartControl(Composite parent) {
        parent.setLayout(new FillLayout());
        ISourceViewer viewer;
        int styles = SWT.H_SCROLL | SWT.V_SCROLL;
        document = new Document();
        IPreferenceStore store = JavaPlugin.getDefault().getCombinedPreferenceStore();
        viewer = new JavaSourceViewer(parent, null, null, false, styles, store);
        viewer.setDocument(document);
        JavaTextTools tools = JavaPlugin.getDefault().getJavaTextTools();
        tools.setupJavaDocumentPartitioner(viewer.getDocument(), IJavaPartitions.JAVA_PARTITIONING);
        SimpleJavaSourceViewerConfiguration config = new SimpleJavaSourceViewerConfiguration(tools.getColorManager(),
                store, null, IJavaPartitions.JAVA_PARTITIONING, true);
        JavaSourceViewerConfiguration configure = new JavaSourceViewerConfiguration(tools.getColorManager(), store,
                null, IJavaPartitions.JAVA_PARTITIONING);
        viewer.configure(config);
        viewer.getTextWidget().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT));
        document = viewer.getDocument();
        viewer.setEditable(false);
        getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(this);
        createMenu();
    }

    private void createMenu() {
        IMenuManager manager = getViewSite().getActionBars().getMenuManager();

        manager.add(viewStartAction);
        manager.add(viewMainAction);
        manager.add(viewEndAction);
        manager.add(viewAllAction);
        viewStartAction.setChecked(true);
        viewMainAction.setChecked(true);
        viewEndAction.setChecked(true);
        viewAllAction.setChecked(true);

        viewStartAction.setChecked(false);
        viewMainAction.setChecked(true);
        viewEndAction.setChecked(false);
        viewAllAction.setChecked(false);

    }

    @Override
    public void setFocus() {

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
     * org.eclipse.jface.viewers.ISelection)
     */
    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
        selectedNode = null;
        if ((part instanceof ProcessMultiEditor) && (selection instanceof IStructuredSelection)) {
            IStructuredSelection ss = (IStructuredSelection) selection;
            if (ss.toList().size() == 1) {
                Object fe = ss.getFirstElement();
                if (fe instanceof CompNodePart) {
                    selectedNode = (CompNode) ((CompNodePart) fe).getModel();
                }
            }
        }
        refresh();
    }

    public void refresh() {
        String gcc = ""; //$NON-NLS-1$
        if (selectedNode != null) {
            ICodeGenerator codeGenerator = CorePlugin.getDefault().getDesignerCoreService().getCodeGenerator();
            switch (selectedCode) {
            case CODE_START:
                gcc = codeGenerator.generateComponentCode(selectedNode, EJETCodePart.BEGIN);
                break;
            case CODE_MAIN:
                gcc = codeGenerator.generateComponentCode(selectedNode, EJETCodePart.MAIN);
                break;
            case CODE_END:
                gcc = codeGenerator.generateComponentCode(selectedNode, EJETCodePart.END);
                break;
            case CODE_ALL:
                gcc = codeGenerator.generateComponentCode(selectedNode, EJETCodePart.BEGIN);
                gcc += codeGenerator.generateComponentCode(selectedNode, EJETCodePart.MAIN);
                gcc += codeGenerator.generateComponentCode(selectedNode, EJETCodePart.END);
                break;
            }
        }
        viewStartAction.setChecked(false);
        viewMainAction.setChecked(false);
        viewEndAction.setChecked(false);
        viewAllAction.setChecked(false);
        switch (selectedCode) {
        case CODE_START:
            viewStartAction.setChecked(true);
            break;
        case CODE_MAIN:
            viewMainAction.setChecked(true);
            break;
        case CODE_END:
            viewEndAction.setChecked(true);
            break;
        case CODE_ALL:
            viewAllAction.setChecked(true);
            break;
        }
        document.set(gcc);
    }
}
